require("iuplua")
require("iupluagl")
require("luagl")
require("luaglu")

iup.key_open()

rtri = 0
rquad = 0
xrot, yrot, zrot = 0, 0, 0

cnv = iup.glcanvas{buffer="DOUBLE", rastersize = "640x480"}

timer = iup.timer{time=10}

function timer:action_cb()
  rtri = rtri + 0.2             -- Increase The Rotation Variable For The Triangle ( NEW )
  rquad = rquad - 1.5
  xrot = rquad
  iup.Update(cnv)
end

function cnv:resize_cb(width, height)
  iup.GLMakeCurrent(self)
  gl.Viewport(0, 0, width, height)

  gl.MatrixMode('PROJECTION')   -- Select The Projection Matrix
  gl.LoadIdentity()             -- Reset The Projection Matrix
  
  if height == 0 then           -- Calculate The Aspect Ratio Of The Window
    height = 1
  end

  glu.Perspective(80, width / height, 1, 5000)

  gl.MatrixMode('MODELVIEW')    -- Select The Model View Matrix
  gl.LoadIdentity()             -- Reset The Model View Matrix
end

function cnv:action(x, y)
  iup.GLMakeCurrent(self)
  gl.Clear('COLOR_BUFFER_BIT,DEPTH_BUFFER_BIT') -- Clear Screen And Depth Buffer
  
  gl.LoadIdentity()             -- Reset The Current Modelview Matrix
  gl.Translate(-1.5, 0, -6)     -- Move Left 1.5 Units And Into The Screen 6.0
        -- Rotate The Triangle On The Y axis ( NEW )

  gl.LoadIdentity()             -- Reset The Current Modelview Matrix
  gl.Translate(1.5, 0, -7)      -- Move Right And Into The Screen
  gl.Rotate(rquad, 0, 1, 0)      -- Rotate The Triangle On The Y axis ( NEW )
  -- gl.Rotate(rquad, 1, 0, 0)      -- Rotate The Triangle On The Y axis ( NEW )
  -- gl.Rotate(rquad, 0, 0, 1)
  
  gl.Color (0.5, 0.5, 1)        -- Set The Color To Blue One Time Only
  if matrizDeRotacao(0, 0, 1) >= 0 then
  gl.Begin('QUADS')             -- Draw A Quad
    gl.Vertex(-1, 1, 0)         -- Top Left
    gl.Vertex( 1, 1, 0)         -- Top Right
    gl.Vertex( 1,-1, 0)         -- Bottom Right
    gl.Vertex(-1,-1, 0)         -- Bottom Left
  gl.End()
  end

  iup.GLSwapBuffers(self)
end

function cnv:k_any(c)
  if c == iup.K_q or c == iup.K_ESC then
    return iup.CLOSE
  elseif c == iup.K_F1 then
    if fullscreen then
      fullscreen = false
      dlg.fullscreen = "No"
    else
      fullscreen = true
      dlg.fullscreen = "Yes"
    end
  end
end

function matrizDeRotacao(a, b, c)
	resultx = math.cos(yrot*math.pi/180)*math.cos(zrot*math.pi/180)*a + (math.sin(xrot*math.pi/180)*math.sin(yrot*math.pi/180)*math.cos(zrot*math.pi/180) + math.cos(xrot*math.pi/180)*math.sin(zrot*math.pi/180))*b + (-math.cos(xrot*math.pi/180)*math.sin(yrot*math.pi/180)*math.cos(zrot*math.pi/180) + math.sin(xrot*math.pi/180)*math.sin(zrot*math.pi/180))*c
	resulty = - math.cos(yrot*math.pi/180)*math.sin(zrot*math.pi/180)*a + (-math.sin(xrot*math.pi/180)*math.sin(yrot*math.pi/180)*math.sin(zrot*math.pi/180) + math.cos(xrot*math.pi/180)*math.cos(yrot*math.pi/180))*b + (math.cos(xrot*math.pi/180)*math.sin(yrot*math.pi/180)*math.sin(zrot*math.pi/180) + math.sin(xrot*math.pi/180)*math.cos(yrot*math.pi/180))*c
	resultz =  math.sin(yrot*math.pi/180)*a - math.sin(xrot*math.pi/180)*math.cos(yrot*math.pi/180)*b + math.cos(xrot*math.pi/180)*math.cos(yrot*math.pi/180)*c
	print(resultx .. " " .. resulty .. " " .. resultz .. " " .. xrot .. " " .. yrot  .. " " .. zrot)
	return resultz
end

function cnv:map_cb()
  iup.GLMakeCurrent(self)
  gl.ShadeModel('SMOOTH')            -- Enable Smooth Shading
  gl.ClearColor(0, 0, 0, 0.5)        -- Black Background
  gl.ClearDepth(1.0)                 -- Depth Buffer Setup
  gl.Enable('DEPTH_TEST')            -- Enables Depth Testing
  gl.DepthFunc('LEQUAL')             -- The Type Of Depth Testing To Do
  gl.Enable('COLOR_MATERIAL')
  gl.Hint('PERSPECTIVE_CORRECTION_HINT','NICEST')
end

function CalculateNormal(param_a, param_b, param_c, param_d, param_e, param_f)
	local k = 0.2
	local param_x, param_y, param_z = k*(param_d - param_a), k*(param_e - param_b), k*(param_f - param_c)
	local amplitude0 = math.sqrt(param_x^2 + param_y^2 + param_z^2)
	local prov1_x, prov1_y, prov1_z
	local amplitude
	local prov2_x, prov2_y, prov2_z
	
	if seta then
		gl.Begin("LINES")
		gl.Color(1, 1, 1)
		gl.Vertex(param_a, param_b, param_c)
		gl.Vertex(param_d, param_e, param_f)
		gl.End()
	
		if true then
			if param_y == 0 and param_z == 0 then
				prov1_x, prov1_y, prov1_z = param_y, -param_x, 0
			else
				prov1_x, prov1_y, prov1_z = 0, param_z, -param_y
			end
			amplitude = math.sqrt(prov1_x^2 + prov1_y^2 + prov1_z^2)
			prov1_x, prov1_y, prov1_z = prov1_x * amplitude0 / amplitude, prov1_y * amplitude0 / amplitude, prov1_z * amplitude0 / amplitude 
			prov2_x, prov2_y, prov2_z = VectorProduct(prov1_x, prov1_y, prov1_z, param_x, param_y, param_z)
			amplitude = math.sqrt(prov2_x^2 + prov2_y^2 + prov2_z^2)
			prov2_x, prov2_y, prov2_z = prov2_x * amplitude0 / amplitude, prov2_y * amplitude0 / amplitude, prov2_z * amplitude0 / amplitude
			
			gl.Begin("TRIANGLES")
			gl.Color ( 1, 1, 1)         -- White
			gl.Vertex( param_d + param_x, param_e + param_y, param_f + param_z ) -- TOP
			gl.Vertex( param_d + prov1_x, param_e + prov1_y, param_f + prov1_z ) -- VERTEX 1
			gl.Vertex( param_d + prov2_x, param_e + prov2_y, param_f + prov2_z ) -- VERTEX 2
			gl.Vertex( param_d + param_x, param_e + param_y, param_f + param_z ) -- TOP
			gl.Vertex( param_d + prov2_x, param_e + prov2_y, param_f + prov2_z ) -- VERTEX 2
			gl.Vertex( param_d - prov1_x, param_e - prov1_y, param_f - prov1_z ) -- VERTEX 3
			gl.Vertex( param_d + param_x, param_e + param_y, param_f + param_z ) -- TOP
			gl.Vertex( param_d - prov1_x, param_e - prov1_y, param_f - prov1_z ) -- VERTEX 3
			gl.Vertex( param_d - prov2_x, param_e - prov2_y, param_f - prov2_z ) -- VERTEX 4
			gl.Vertex( param_d + param_x, param_e + param_y, param_f + param_z ) -- TOP
			gl.Vertex( param_d - prov2_x, param_e - prov2_y, param_f - prov2_z ) -- VERTEX 4
			gl.Vertex( param_d + prov1_x, param_e + prov1_y, param_f + prov1_z ) -- VERTEX 1
			gl.End()			
			gl.Begin("QUADS")
			gl.Vertex( param_d + prov1_x, param_e + prov1_y, param_f + prov1_z ) -- VERTEX 1
			gl.Vertex( param_d + prov2_x, param_e + prov2_y, param_f + prov2_z ) -- VERTEX 2
			gl.Vertex( param_d - prov1_x, param_e - prov1_y, param_f - prov1_z ) -- VERTEX 3
			gl.Vertex( param_d - prov2_x, param_e - prov2_y, param_f - prov2_z ) -- VERTEX 4
			gl.End()
		end
	end
end

dlg = iup.dialog{cnv; title="LuaGL Test Application 04"}

dlg:show()
cnv.rastersize = nil -- reset minimum limitation
timer.run = "YES"

if (not iup.MainLoopLevel or iup.MainLoopLevel()==0) then
  iup.MainLoop()
end
